<?php
 strtolower(basename($_SERVER['PHP_SELF'])) == 'Comment.class.php' && header('Location:http://' . $_SERVER['HTTP_HOST']); //禁止直接访问本页

require_once 'db.php';
require_once('BeanUtil.php');
require_once('Document.php');

  class Comment{
	public $id;
	public $userid;
    public $content;
    public $docid;
    public $delkb;
    public $wrtdt;
    public $wrttm;
	public $title;
	public $author;
	
	public static  function getCommentList(){
		    $sql="select c.*,d.title,d.author from comment c,document d where c.docid=d.id and c.delkb=0 and d.delkb=0 order by c.wrtdt,c.wrttm ";
		    $mydb=new MySql();
    		$resultArray=$mydb->query($sql);
    		$arr=array();
    		$idx=0;
    		 while($data = $mydb->fetch($resultArray)){
   					$bean = new Comment();
   					BeanUtil::fetch($data, $bean);
   					$arr[$idx++] = $bean;
   		     }
    		return $arr;
	}
	
	public static function getComtById($id=''){
			$sql="select c.*,d.title,d.author from comment c,document d where c.docid=d.id and c.id={$id} and c.delkb=0";
			$mydb=new MySql();
    		$resultArray=$mydb->query($sql);
			$data = $mydb->fetch($resultArray);
			if($data){
				$bean = new Comment();
	   			BeanUtil::fetch($data, $bean);
				return $bean;
			}
			return null;
    }
	
	public static function updateComtById($id='',$upCont=''){
			$upsql="update comment set content='{$upCont}' where id = $id ";
			$mydb=new MySql();
			$exe=$mydb->query($upsql);
			if($exe)
			   return true;
            else
				return false;
    }
	
	
	public static function delComtById($id=''){
			$upsql="UPDATE  comment SET  delkb = 1 WHERE  id ={$id}";
			$mydb=new MySql();
			$exe=$mydb->query($upsql);
			if($exe)
			   return true;
            else
				return false;
    }
	
	public static function addComment($comt){
			if (empty($comt->content))
	    		   return false;
			$arry=array();
			$arry['userid']=$comt->userid;
	        $arry['content']=$comt->content;
	        $arry['docid']=$comt->docid;
	        $arry['wrtdt']=date("Ymd");
	        $arry['wrttm']=date("His");
			$mydb=new MySql();
			//echo $arry;exit;
			$exe=$mydb->addData('comment',$arry);
			if($exe)
			   return true;
	        else
	           return false;
	} 
	
	public static function getDocs(){
		$sql="select d.id,d.title,d.author from document d where d.delkb=0";
		$mydb=new MySql();
        $resultArray=$mydb->query($sql);
    	$arr=array();
    	$idx=0;
    	while($data = $mydb->fetch($resultArray)){
   			$bean = new Document();
   			BeanUtil::fetch($data, $bean);
   			$arr[$idx++] = $bean;
   		}
		return $arr;
	}
	
}
?>
